package com.pocket.demo.core.log;

import ch.qos.logback.core.spi.DeferredProcessingAware;
import com.fasterxml.jackson.core.JsonGenerator;
import net.logstash.logback.composite.AbstractFieldJsonProvider;
import net.logstash.logback.composite.FieldNamesAware;
import net.logstash.logback.composite.JsonWritingUtils;
import net.logstash.logback.fieldnames.LogstashCommonFieldNames;
import org.apache.commons.lang3.math.NumberUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


public class CumtomJsonProvider<Event extends DeferredProcessingAware> extends AbstractFieldJsonProvider<Event> implements FieldNamesAware<LogstashCommonFieldNames> {
  public void setFieldNames(LogstashCommonFieldNames fieldNames) {}
  
  public void writeTo(JsonGenerator generator, Event event) throws IOException {
    Map<String, Object> map = new HashMap<>();
    map.put("host_name", ServerHostNameConverter.getHostName());
    map.put("ip", ServerIpConverter.getIp());
    map.put("port", Integer.valueOf(ServerPortConverter.getPort()));
    map.put("pid", Long.valueOf(NumberUtils.toLong(PidConverter.getPid())));
    map.put("thread_id", Long.valueOf(ThreadIdConverter.getThreadId()));
    map.put("req_id", RequestIdConverter.getReqId());
    JsonWritingUtils.writeMapEntries(generator, map);
  }
}
